/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luedinski.objects;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import org.jgrapht.GraphPath;
import org.jgrapht.Graphs;

/**
 *
 * @author lueder
 */
public class KlassenTest {

    public static void main(String[] args) {
        // Test
        
        
        
        // Leeres Gleisnetz
        Net net = new Net();
        
        DecimalFormat f = new DecimalFormat("#0.00");

        // Stationen aus DB
        Station hb = new Station(1, "Bremen", 53.1094, 8.78139);
        Station hh = new Station(2, "Hamburg", 53.5544, 9.99458);
        Station h = new Station(3, "Hannover", 52.3667, 9.71667);
        Station b = new Station(4, "Berlin", 52.5167, 13.4);
        Station k = new Station(5, "Kiel", 54.3333, 10.1333);
        Station w = new Station(6, "Wolfsburg", 52.4333, 10.8);

        net.addConnection(hb, hh);
        net.addConnection(hh, hb);
        
        net.addConnection(hb, h);
        net.addConnection(h, hb);
        
        net.addConnection(hh, b);
        net.addConnection(b, hh);
        
        net.addConnection(h, w);
        net.addConnection(w, h);
        
        net.addConnection(hh, k);
        net.addConnection(k, hh);
        
        net.addConnection(hh, h);
        net.addConnection(h, hh);
        
        net.addConnection(w, b);
        net.addConnection(b, w);
        
        System.out.println("Gleisnetz:");
        System.out.println(net.toString()+"\n");
        

        // Züge
        HashMap<Integer, Route> routesMap = new HashMap<Integer, Route>();

        // temporäre Stations für Züge
        ArrayList<Station> routeStations = new ArrayList<Station>();

        routeStations.add(hb);
        routeStations.add(hh);
        routeStations.add(b);
        Route routeA = new Route(1, "ICE 325", 250, routeStations, net.getNet());
        routeA.generateTimetable(new Time(6,0), new Time(23,0), 90);
        routeA.printRoute();
        routesMap.put(routeA.getId(), routeA);
        

        routeStations = new ArrayList<Station>();
        routeStations.add(hb);
        routeStations.add(h);
        routeStations.add(w);
        routeStations.add(b);
        Route routeB = new Route(2, "IC 235", 150, routeStations, net.getNet());
        routeA.generateTimetable(new Time(6,30), new Time(23, 30), 60);
        routeB.printRoute();
        routesMap.put(routeB.getId(), routeB);

        routeStations = new ArrayList<Station>();
        routeStations.add(hh);
        routeStations.add(k);
        Route routeC = new Route(3, "RE 523", 120, routeStations, net.getNet());
        routeA.generateTimetable(new Time(6,30), new Time(23, 30), 45);
        routeC.printRoute();
        routesMap.put(routeC.getId(), routeC);
        
        // Fahplan:
        Schedule schedule = new Schedule();
        for(int i = 6; i<24; i++){
            schedule.addTrain(routeA, new Time(i, 30));
            schedule.addTrain(routeB, new Time(i, 45));
            schedule.addTrain(routeC, new Time(i, 0));
        }
        //schedule.printSchedule();
        
        Trip trip = new Trip();
        trip.generateTrips(hb, k, new Time(10,35), net, routesMap);

        /*// Start, Ziel, Startzeit:
        Station origin = hb, destination = k;
        Time departure = new Time(10,35);
        System.out.println("Abfahrt: "+departure.getTime());
        System.out.println("Luftlinine: " + origin.getName() + " -> " + destination.getName() + ": " + f.format(Station.computeGeoDistance(origin, destination))+"\n");

        // Alle möglichen Wege
        Iterator<GraphPath<Station, Station>> paths = net.getRoutes(origin, destination, 4);
        int i = 0;
        while (paths.hasNext()) {
            GraphPath<Station, Station> path = paths.next();

            System.out.println((++i) + ".: " + path.getStartVertex().getName() + " -> " + path.getEndVertex().getName());

            // Geordnete Liste aller Stationen eines Weges
            Iterator<Station> stationIter = Graphs.getPathVertexList(path).iterator();
            Station actual = null;
            Station prev = null;
            double distance = 0;
            int stops = 0;

            // Alle Stations eines Weges ablaufen:
            while (stationIter.hasNext()) {
                stops++;
                // Aktuelle Station
                actual = stationIter.next();
                // Erst aber der 2. Station berechnen (1. Edge)
                if (prev != null) {
                    // Gesamtdistanz erhöhen
                    distance += net.getDistance(prev, actual);
                    System.out.println(prev.getName() + " -> " + actual.getName());
                    
                    // Züge finden ohne Zeit!
                    Iterator<Route> routesCol = routesMap.values().iterator();
                    while(routesCol.hasNext()){
                        Route route = routesCol.next();
                        if(route.containsEdge(prev, actual)){
                            System.out.println(route.toString());
                        }
                    }*/
                    
                    // Züge mit Zeiten finden
                    /*ArrayList<ScheduledRoute> possibleTrains = schedule.getTrains(prev, actual, departure);
                    for (int j = 0; j < possibleTrains.size(); j++) {
                        System.out.println(j+": "+possibleTrains.get(j).toString(prev));
                        //System.out.print(("R: "+(routesList.get(j).containsEdge(prev, actual) ? routesList.get(j).getName() + "\n" : "")));
                    }*/
                /*}
                prev = actual;
            }
            System.out.println("Distance: " + f.format(distance));
            System.out.println("Stops: " + (stops - 2) + "\n");

        }*/

    }
}
